import axios from "axios";
import store from '../store'


//使用axios.create()创建一个axios实例，该实例与axios几乎一致
//可以在创建实例时设置baseUrl
export const host = process.env.NODE_ENV === 'development' ? 'http://120.76.247.5:2002' : 'http://120.76.247.5:2002'
const request = axios.create({
    baseURL: host +'/api',
    // timeout: 1000,
    //指定请求头
    // headers: {'X-Custom-Header': 'foobar'}
});



request.interceptors.request.use( (config)=> {
    
    if (['delete','post','put','patch'].includes(config.method)) {
        const { userInfo:{authorization}} = store.getState();
        config.headers['Authorization'] = authorization;
    }
    // 在发送请求之前做些什么
    return config;
},  (error)=> {
    // 对请求错误做些什么
    return Promise.reject(error);
});

export default request